package com.zfjt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.NewsBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.packnum.PackNum00032;
import com.zfjt.service.NewsBeanService;

/**
 * 发布新闻接口
 * @author xk
 * @version 1.0
 * @date 2017年3月29日
 */
@Controller
public class PublishNewsController {
	
	//返回对象
	private ReturnBean mReturn;
	
	private Map<String, String> fhdata;
		
	@Autowired
	private NewsBeanService mNews;
	
	@RequestMapping(value="00032")
	@ResponseBody
	public ReturnBean PublishNews(HttpSession hseesion){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00032 num00032 = JSON.parseObject(data,PackNum00032.class); 	
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {
			mReturn.setPortnum("00032");
			mReturn.setCode("001");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			List<NewsBean> mData = mNews.selectAll();
			System.out.println("序号==========" + mData.get(mData.size() - 1).getId());
			int id = Integer.parseInt(mData.get(mData.size() - 1).getId()) + 1;
			mNews.insert(new NewsBean(id + "", num00032.getTitle(), 
					num00032.getDetails(), num00032.getIssuer(), num00032.getIssuer_id(), num00032.getAdd_time()));		
//			List mUserData = mUser.selectAll();
//			for (int i = 0; i < mUserData.size(); i++) {
//				UserBean dg = (UserBean) mUserData.get(i);
//				String str = dg.getNews();
//				dg.setNews(str + "," + mData.size());
//				mUser.updateByPrimaryKeySelective(dg);
//			}
			mReturn.setCode("000");
			mReturn.setData("000");
			mReturn.setPortnum("00032");
		}
		return mReturn;
	}
}
